package com.sctvcloud.qingchuan.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.sctvcloud.qingchuan.beans.FocusColumn;
import com.sctvcloud.qingchuan.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.qingchuan.ui.fragment.PoliticsListFragment;
import com.sctvcloud.qingchuan.ui.fragment.TvFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * 问政
 */
public class PoliticsFormPageAdapter extends FragmentStatePagerAdapter {

    public static final int TYPE_NEW_PUBLISH = 0;//最新发布
    public static final int TYPE_HOTS_POLITICS = 1;//最热问政
    public static final int TYPE_MINE_POLITICS = 2;//我的问政
    private List<FocusColumn> list;
    private TvFragment tv;
    private FocusColumn specail;
    private FocusColumn anchor;
    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    private int topHeight;
    private boolean is_mine;

    public boolean isIs_mine() {
        return is_mine;
    }

    public void setIs_mine(boolean is_mine) {
        this.is_mine = is_mine;
    }

    public PoliticsFormPageAdapter(FragmentManager fm, List<FocusColumn> list) {
        super(fm);
        this.list = list;
        fragmentWeaks = new SparseArray<>();
    }

    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        switch (list.get(position).getChannelType()) {
            case "" + TYPE_NEW_PUBLISH:
                fragment = new PoliticsListFragment();
                ((PoliticsListFragment) fragment).setPoliticsType(TYPE_NEW_PUBLISH);
                break;
            case "" + TYPE_HOTS_POLITICS:
                fragment = new PoliticsListFragment();
                ((PoliticsListFragment) fragment).setPoliticsType(TYPE_HOTS_POLITICS);
                break;
            case "" + TYPE_MINE_POLITICS:
                fragment = new PoliticsListFragment();
                ((PoliticsListFragment) fragment).setPoliticsType(TYPE_MINE_POLITICS);
                break;
            default:
                fragment = new PoliticsListFragment();
                break;
        }

        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(fragment));
        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getChannelName();
    }

    public void setData(List<FocusColumn> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    public int getPosCurrentPage(String type) {
        if (getCount() == 0) {
            return -1;
        }
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getChannelType().equals(type)) {
                return i;
            }
        }
        return -1;
    }

    public void setTopHeight(int topHeight) {
        this.topHeight = topHeight;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }


}
